//引入express mvc框架
const express = require("express");
const db = require('../modules/DBhelper');
const moment = require('moment');
//创建路由
let router = express.Router();

//查看某个商品
router.get('/see', async function (request, response, next) {
    //获取前台数据
    let gId = request.query.gId;
    let sql = "SELECT * FROM `goods` WHERE `g_id`=? ;";
    let param = [gId];
    try {
        let result = await db.exec(sql, param);
        let isSelected = result && result.length >= 0;
        response.json({
            msg: isSelected ? '查询成功' : '查询失败',
            status: isSelected ? 200 : -200,
            data: result
        })
    } catch (e) {
        response.json({
            msg: '查询失败',
            status: -200,
            e
        });
    }
})

//查看全部商品
router.get('/seeAll', async function (request, response, next) {
    let sql = 'SELECT * FROM `goods` ;';
    try {
        let result = await db.exec(sql);
        let isSuccess = result && result.length >= 0;
        response.json({
            msg: isSuccess ? '查询成功' : '查询失败',
            status: isSuccess ? 200 : -200,
            data: result
        })
    } catch (e) {
        response.json({
            msg: '查询失败',
            status: -200,
            e
        });
    }

})

//暴露出去
module.exports = {router};